package service.edu.controller.index;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import service.edu.entity.Course;
import service.edu.entity.chapter.ChapterVo;
import service.edu.entity.vo.IndexCourseAll;
import service.edu.entity.vo.IndexCourseVo;
import service.edu.result.R;
import service.edu.service.ChapterService;
import service.edu.service.CourseService;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RequestMapping("/edu/indexCourse")
@RestController
public class IndexCourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private ChapterService chapterService;

    @ApiOperation("首页课程条件分页查询")
    @PostMapping("/pageQuery/condition/{current}/{limit}")
    public R pageQuery(@ApiParam(name = "current", value = "当前页码", required = true)
                       @PathVariable("current")long current,
                       @ApiParam(name = "limit", value = "每页记录数", required = true)
                       @PathVariable("limit")long limit,
                       @ApiParam(name = "indexCourseVo",value = "查询的条件对象") @RequestBody(required = false)IndexCourseVo indexCourseVo){
        Page<Course> page = new Page<>(current,limit);
        HashMap<String,Object> map=courseService.getIndexCourseListByCondition(page,indexCourseVo);
        return R.Ok().data(map);
    }

    @ApiOperation("通过sql语句并根据课程id查询课程详情")
    @GetMapping("/queryInfo/{courseId}")
    public R queryInfoByCourseId(@PathVariable(value = "courseId")String courseId){
        IndexCourseAll indexCourseAll=courseService.getIndexCourseInfoByCourseId(courseId);
        List<ChapterVo> chapterAndVideoList = chapterService.getAllChapters(courseId);
        return R.Ok().data("chapterAndVideoList",chapterAndVideoList).data("indexCourseInfo",indexCourseAll);
    }
}
